package com.aerozhonghuan.logisticsmall.framework.DB;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

import com.aerozhonghuan.logisticsmall.modules.common.entity.GoodsObject;
import com.aerozhonghuan.logisticsmall.modules.common.entity.SearchProductHistory;
import com.aerozhonghuan.logisticsmall.modules.home.beans.ProductFavorite;
import com.aerozhonghuan.logisticsmall.utils.CommunityTool;

import java.util.ArrayList;
import java.util.Collections;

/**
 * Created by ChenLin on 2019/5/21 15:13.Email&WeChat:7877409@163.com
 */
public class DBAdapter {

    private static final String TAG = "community2";
    private static final String DATABASE_NAME = "kydb";
    private static final String DATABASE_TABLE_PRODUCTS = "t_products"; // 商品
    private static final String DATABASE_TABLE_PRODUCT_CATEGORY = "t_product_category"; // 商品大类型
    private static final String DATABASE_TABLE_PRODUCT_SHOPPING_CAR = "t_product_shopping_car"; // 购物车
    private static final String DATABASE_TABLE_DICTTYPE = "t_dict_type"; // 字典类型
    private static final String DATABASE_TABLE_DICTDATA_PAY = "t_dict_data_pay";// 根据字典类型id查询字典数据
    private static final String DATABASE_TABLE_DICTDATA_ORDERSTATUS = "t_dict_data_orderstatus";// 根据字典类型id查询字典数据
    private static final String DATABASE_TABLE_DICTDATA_PRODUCT_BRAND = "t_dict_data_product_brand";// 根据字典类型id查询字典数据 商品品牌
    private static final String DATABASE_TABLE_PRODUCT_FAVORITE = "t_product_favarite";// 收藏商品
    private static final String DATABASE_TABLE_DELIVERY_ADDRESS = "t_delivery_address";// 配送详细地址
    private static final String DATABASE_TABLE_DELIVERY_AREA = "t_delivery_area";// 配送区域表
    private static final String DATABASE_TABLE_DELIVERY_COMMUNITY = "t_delivery_community";// 配送小区表
    private static final String DATABASE_TABLE_SEARCH_PRODUCT_HISTORY = "t_search_product_history";// 配送小区表
    private static final String DATABASE_TABLE_PRODUCT_IMGS = "t_product_imgs";// 商品图片
    private static final String DATABASE_TABLE_BANNER = "t_banner";// banner 表
    private static final String DATABASE_TABLE_CHAT_MSG = "t_chat_msg";// chat聊天记录表
    /**
     * version==9 简历chat表，购物车中表中添加product_type字段
     */
    private static final int DATABASE_VERSION = 9;
    private Context mContext;
    private static DatabaseHelper mDBHelper;
    private SQLiteDatabase mDb;

    public DBAdapter(Context context) {
        this.mContext = context;
        this.mDBHelper = new DatabaseHelper(mContext);
    }

    public ProductFavorite getFavoriteFromId(int productid) {
        mDb = mDBHelper.getReadableDatabase();
        Log.d(TAG, "productid==>:" + productid);
        Cursor cursor = mDb.rawQuery("select * from "
                + DATABASE_TABLE_PRODUCT_FAVORITE + " where product_id=?", new String[]{String.valueOf(productid)});
        ProductFavorite favorite = null;
        if (cursor != null) {
            while (cursor.moveToNext()) {
                favorite = new ProductFavorite();
                Log.d(TAG, "movetonext==>:" + productid);
                favorite.setFavorite_id(cursor.getInt(0));
                favorite.setProduct_id(cursor.getInt(1));
                favorite.setBar_code(cursor.getInt(2));
                favorite.setProduct_stamp(cursor.getInt(3));
                favorite.setProduct_name(cursor.getString(4));
                favorite.setProduct_category(cursor.getInt(5));
                favorite.setProduct_img(cursor.getInt(6));
                favorite.setProduct_price(cursor.getFloat(7));
                favorite.setProduct_brand(cursor.getString(8));
            }
            cursor.close();
            mDb.close();
            return favorite;
        }
        return null;
    }

    /**
     * 删除搜索历史数据by key
     */
    public boolean deleteSearchProductByKeyData(String key) {
        return mDb.delete(DATABASE_TABLE_SEARCH_PRODUCT_HISTORY, "product_name" + "=?",
                new String[]{key}) > 0;
    }

    /**
     * 插入查询商品历史数据
     */
    public long insertSearchProductHistory(long timeId, String keyword) {
        long historyid = -1;
        try {
            mDb = mDBHelper.getWritableDatabase();
            ContentValues values = new ContentValues();
            values.put("time_id", timeId);
            values.put("product_name", keyword);
            historyid = mDb.insert(DATABASE_TABLE_SEARCH_PRODUCT_HISTORY, null,
                    values);
            if (mDb != null) {
                mDb.close();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return historyid;
    }


    /**
     * 删除一个标题:商品删除
     */
    public boolean deleteProductForId(String id) {
        return mDb.delete(DATABASE_TABLE_PRODUCTS, "id" + "=?",
                new String[]{id}) > 0;
    }

    /**
     * 删除一个标题--购物车商品删除
     */
    public boolean deleteTitle(String id) {
        return mDb.delete(DATABASE_TABLE_PRODUCT_SHOPPING_CAR, "product_id" + "=?",
                new String[]{id}) > 0;
    }

    /**
     * 删除收藏商品
     */
    public boolean deleteFavoritTitle(String id) {
        return mDb.delete(DATABASE_TABLE_PRODUCT_FAVORITE,
                "favorite_id" + "=?", new String[]{id}) > 0;
    }

    /**
     * 删除搜索历史数据by id
     */
    public boolean deleteSearchProductData(String id) {
        return mDb.delete(DATABASE_TABLE_SEARCH_PRODUCT_HISTORY, "time_id" + "=?",
                new String[]{id}) > 0;
    }

    /**
     * 查询搜索商品历史数据
     */
    public ArrayList<SearchProductHistory> querySearchProductHistory() {
        if (com.blankj.utilcode.util.AppUtils.isAppDebug())
            Log.d(TAG, "querySearchProductHistory");
        mDb = mDBHelper.getReadableDatabase();
        Cursor cursor = mDb.rawQuery("select * from "
                + DATABASE_TABLE_SEARCH_PRODUCT_HISTORY, null);
        if (cursor != null) {
            int columns = cursor.getColumnCount();
            ArrayList<SearchProductHistory> keyList = new ArrayList<SearchProductHistory>();
            if (com.blankj.utilcode.util.AppUtils.isAppDebug())
                Log.d(TAG,
                        "COLUMNS===>" + columns + "count:" + cursor.getCount());
            while (cursor.moveToNext()) {
                SearchProductHistory data = new SearchProductHistory();
                data.setTimeId(cursor.getLong(0));
                data.setProcutName(cursor.getString(1));
                keyList.add(data);
            }
            cursor.close();
            mDb.close();
            return keyList;
        }
        return null;
    }


    /**
     * 更新一个标题 购物车
     */
    public int updateGouwuche(int productId, int userId, String object) {
        ContentValues values = new ContentValues();
        values.put("product_id", productId);
        values.put("user_id", userId);
        values.put("goods_object", object);
        return mDb.update(DATABASE_TABLE_PRODUCT_SHOPPING_CAR, values, "product_id"
                + "=" + productId, null);
    }

    /**
     * 插入一个商品的数据--购物车
     */
    public long insertGouwuche(int product_id, int user_id, String objectStr, int product_type) {
        if (com.blankj.utilcode.util.AppUtils.isAppDebug())
            Log.d(TAG, "insert data");
        long id = -1;
        try {
            mDb = mDBHelper.getWritableDatabase();
            ContentValues values = new ContentValues();
            values.put("product_id", product_id);
            values.put("user_id", user_id);
            values.put("product_type", product_type);
            values.put("goods_object", objectStr);
            id = mDb.insert(DATABASE_TABLE_PRODUCT_SHOPPING_CAR, null, values);
            if (mDb.isOpen()) {
                mDb.close();
            }
            if (com.blankj.utilcode.util.AppUtils.isAppDebug())
                Log.d(TAG, "long==>: " + id);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return id;
    }


    // 查询商品信息gouwuche
    public ArrayList<GoodsObject> getAllData() {
        Log.d(TAG, "getAllData");
        mDb = mDBHelper.getReadableDatabase();
        Cursor cursor = mDb.rawQuery("select * from "
                + DATABASE_TABLE_PRODUCT_SHOPPING_CAR, null);
        int columns = cursor.getColumnCount();
        ArrayList<GoodsObject> list = new ArrayList<GoodsObject>();
        ArrayList<GoodsObject> notEnoughList = new ArrayList<GoodsObject>();
        Log.d(TAG, "columns:" + columns);
        while (cursor.moveToNext()) {
            String pString = cursor.getString(2);
            GoodsObject goodObject = (GoodsObject) CommunityTool.readObject(pString);
            if (goodObject != null) {
                if (goodObject.getpNotEnough() == 1) {
                    notEnoughList.add(goodObject);
                } else {
                    list.add(goodObject);
                }
            }
        }
        list.addAll(notEnoughList);
        Collections.reverse(list);
        return list;
    }


    /**
     * 打开数据库
     */
    public DBAdapter open() throws SQLException {
        if (com.blankj.utilcode.util.AppUtils.isAppDebug())
            Log.d(TAG, "Database  open()");
        mDb = mDBHelper.getWritableDatabase();
        return this;
    }

    private static class DatabaseHelper extends SQLiteOpenHelper {

        public DatabaseHelper(Context context) {
            super(context, DATABASE_NAME, null, DATABASE_VERSION);
            if (com.blankj.utilcode.util.AppUtils.isAppDebug())
                Log.d(TAG, "DatabaseHelper()");
        }

        @Override
        public void onCreate(SQLiteDatabase db) {
            if (com.blankj.utilcode.util.AppUtils.isAppDebug())
                Log.d(TAG, "DATABASE CREATE");
            db.execSQL("CREATE TABLE " + DATABASE_TABLE_PRODUCTS + " ( "
                    + "id" + " integer NOT NULL PRIMARY KEY AUTOINCREMENT,"
                    + "product_name varchar(255) DEFAULT NULL,"
                    + "product_stamp bigint(255) DEFAULT NULL,"
                    + "product_category int(11) DEFAULT NULL,"
                    + "product_weight int(11) DEFAULT NULL,"
                    + "product_price float DEFAULT NULL,"
                    + "product_in_price float DEFAULT NULL,"
                    + "product_brand varchar(255) DEFAULT NULL,"
                    + "product_spec varchar(255) DEFAULT NULL,"
                    + "in_spec varchar(255) DEFAULT NULL,"
                    + "delivery_spec varchar(255) DEFAULT NULL,"
                    + "product_unit varchar(255) DEFAULT NULL,"
                    + "manufacturer varchar(255) DEFAULT NULL,"
                    + "address varchar(255) DEFAULT NULL,"
                    + "keep_day int(255) DEFAULT NULL,"
                    + "is_ret int(8) DEFAULT NULL,"   //'是否可退货(0：不可退；1：可退)'
                    + "supplier varchar(255) DEFAULT NULL,"   //供应商
                    + "product_description varchar(500) DEFAULT NULL,"
                    + "quantity_stock int(11) DEFAULT NULL,"
                    + "quantity_reserved int(11) DEFAULT NULL,"
                    + "quantity_available int(11) DEFAULT NULL,"
                    + "quantity_unavailable int(11) DEFAULT NULL,"
                    + "quantity_warn int(11) DEFAULT NULL,"
                    + "create_time bigint(20) DEFAULT NULL,"
                    + "delivery_pack varchar(255) DEFAULT NULL,"
                    + "in_pack varchar(255) DEFAULT NULL,"
                    + "bar_code bigint(16) DEFAULT NULL"
                    + " );");
            // 商品大类型
            db.execSQL("CREATE TABLE " + DATABASE_TABLE_PRODUCT_CATEGORY + "("
                    + "id integer primary key not null,"
                    + "category_name text,"
                    + "category_img" + " text,"
                    + "parent_id" + " INTEGER,"
                    + "category_weight INTEGER,"
                    + "f1 text,"
                    + "f2 text,"
                    + "f3 text,"
                    + "f4 text,"
                    + "f5 text,"
                    + "f6 text,"
                    + "f7 text,"
                    + "f8 text"
                    + ");");
            // 商品购物车数据表
            /*db.execSQL("CREATE TABLE " + DATABASE_TABLE_PRODUCT_SHOPPING_CAR + " ( "
                    + "product_id" + " INTEGER PRIMARY KEY NOT NULL,"
                    + "user_id" + " INTEGER NOT NULL,"
                    + "price" + " FLOAT,"
                    + "product_name" + " text,"
                    + "quantity" + " INTEGER NOT NULL,"
                    + "thumbnails" + " text,"    //更新为库存字段
                    + "checks" + " INTEGER NOT NULL,"
                    + "product_stamp integer"
                    + " );");*/
            db.execSQL("CREATE TABLE " + DATABASE_TABLE_PRODUCT_SHOPPING_CAR + " ( "
                    + "product_id" + " INTEGER PRIMARY KEY NOT NULL,"
                    + "user_id" + " INTEGER NOT NULL,"
                    + "goods_object" + " text,"
                    + "product_type" + " INTEGER,"
                    + "temp_field" + " text"
                    + " );");
            // 字典类型表
            db.execSQL("CREATE TABLE "
                    + DATABASE_TABLE_DICTTYPE
                    + "("
                    + "dict_type_id integer not null primary key autoincrement,"
                    + "name text" + ");");
            // 字典数据表-支付类型表
            db.execSQL("CREATE TABLE "
                    + DATABASE_TABLE_DICTDATA_PAY
                    + "("
                    + "dict_data_pay_id integer not null primary key autoincrement,"
                    + "code" + " integer," + "name text," + "description text"
                    + ");");
            // 字典数据表-订单状态表
            db.execSQL("CREATE TABLE "
                    + DATABASE_TABLE_DICTDATA_ORDERSTATUS
                    + "("
                    + "dict_data_orderstatus_id integer,"
                    + "code" + " integer,"
                    + "name text,"
                    + "description text"
                    + ");");
            // 字典数据表-品牌
            db.execSQL("CREATE TABLE "
                    + DATABASE_TABLE_DICTDATA_PRODUCT_BRAND
                    + "("
                    + "dict_data_brand_id integer,"
                    + "code" + " integer DEFAULT NULL,"
                    + "name text DEFAULT NULL,"
                    + "description text DEFAULT NULL"
                    + ");");
            // 商品收藏
            db.execSQL("CREATE TABLE " + DATABASE_TABLE_PRODUCT_FAVORITE
                    + " ( " + "favorite_id" + " INTEGER PRIMARY KEY NOT NULL,"
                    + "product_id" + " INTEGER," + "bar_code" + " INTEGER,"
                    + "product_stamp integer," + "product_name" + " text,"
                    + "product_category" + " INTEGER," + "product_img"
                    + " INTEGER," + "price" + " FLOAT," + "product_brand"
                    + " text," + "user_id" + " INTEGER NOT NULL," + "quantity"
                    + " INTEGER NOT NULL" + " );");
            // 配送地址
            db.execSQL("CREATE TABLE " + DATABASE_TABLE_DELIVERY_ADDRESS
                    + " ( " + "address_id" + " INTEGER PRIMARY KEY NOT NULL,"
                    + "stamp" + " INTEGER NOT NULL," + "contacts" + " text,"
                    + "contact_number" + " text," + "address" + " text,"
                    + "is_default" + " INTEGER," + "createtime text" + " );");
            // 配送区域
            db.execSQL("CREATE TABLE " + DATABASE_TABLE_DELIVERY_AREA + " ( "
                    + "id" + " INTEGER PRIMARY KEY NOT NULL," + "area_name"
                    + " text," + "lng" + " text," + "lat" + " text,"
                    + "parent_id" + " INTEGER," + "description" + " text"
                    + " );");
            // 配送小区
            db.execSQL("CREATE TABLE " + DATABASE_TABLE_DELIVERY_COMMUNITY
                    + " ( " + "id" + " INTEGER PRIMARY KEY NOT NULL,"
                    + "community_name" + " text," + "lng" + " text," + "lat"
                    + " text," + "area_id" + " INTEGER," + "description"
                    + " text" + " );");
            // 搜索商品历史
            db.execSQL("CREATE TABLE " + DATABASE_TABLE_SEARCH_PRODUCT_HISTORY
                    + " ( " + "time_id" + " long PRIMARY KEY NOT NULL,"
                    + "product_name" + " text"
                    + " );");
            // 商品图片
            /*db.execSQL("CREATE TABLE " + DATABASE_TABLE_PRODUCT_IMGS
                    + " ( " + "id NOT NULL,"
                    + "product_id" + " INTEGER DEFAULT NULL,"
                    + "img_id" + " INTEGER"
                    + " );");*/
            db.execSQL("CREATE TABLE " + DATABASE_TABLE_PRODUCT_IMGS
                    + " ( " + "id NOT NULL,"
                    + "product_id" + " INTEGER DEFAULT NULL,"
                    + "img_id" + " INTEGER,"
                    + "is_thumbnail" + " INTEGER"
                    + " );");
            //banner  因为新安装用户没有这个表，在这里创建，老用户需要upgrade来创建这个表
            db.execSQL("CREATE TABLE " + DATABASE_TABLE_BANNER
                    + " ( "
                    + "id INTEGER NOT NULL,"
                    + "action_type text,"
                    + "banner_img text,"
                    + "category_id INTEGER,"
                    + "category_name text,"
                    + "category_img text,"
                    + "parent_id integer,"
                    + "category_weight integer,"
                    + "category_code integer,"
                    + "is_show integer,"
                    + "product_id integer,"
                    + "product_name text,"
                    + "version integer DEFAULT 0"
                    + " );");
            //chat_msg 聊天记录
            db.execSQL("CREATE TABLE " + DATABASE_TABLE_CHAT_MSG + " ( "
                    + "id" + " integer NOT NULL PRIMARY KEY AUTOINCREMENT,"
                    + "message_code" + " text,"
                    + "user_id" + " text,"
                    + "object_entity" + " text,"
                    + "create_time" + " DATETIME,"
                    + "temp_field" + " text"
                    + " );");

        }

        @Override
        public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
            if (com.blankj.utilcode.util.AppUtils.isAppDebug())
                Log.d(TAG, "upgrade database from version " + oldVersion
                        + " to " + newVersion);
            if (newVersion == 3) {
                Log.d(TAG, "upgradeVersion == 3");
                //修改商品类型表中的数据
                db.execSQL("drop TABLE IF EXISTS " + DATABASE_TABLE_PRODUCT_CATEGORY);
                // 商品大类型
                db.execSQL("CREATE TABLE " + DATABASE_TABLE_PRODUCT_CATEGORY + "("
                        + "id integer primary key not null,"
                        + "category_name text,"
                        + "category_img" + " text,"
                        + "parent_id" + " INTEGER,"
                        + "category_weight INTEGER,"
                        + "f1 text,"
                        + "f2 text,"
                        + "f3 text,"
                        + "f4 text,"
                        + "f5 text,"
                        + "f6 text,"
                        + "f7 text,"
                        + "f8 text"
                        + ");");
                Log.d(TAG, "re_create table category done");
                /*//获取到旧表中的字段【也就是新表中保留的字段】
                String columns = getColumnNames( db, DATABASE_TABLE_PRODUCT_SHOPPING_CAR);
                //升级
                upgradeTables( db, DATABASE_TABLE_PRODUCT_SHOPPING_CAR, columns );*/
            } else if (newVersion == 5) {
                // 商品banner  想要添加此表，需要升级数据库
                db.execSQL("CREATE TABLE " + DATABASE_TABLE_BANNER
                        + " ( "
                        + "id INTEGER NOT NULL,"
                        + "action_type text,"
                        + "banner_img text,"
                        + "category_id INTEGER,"
                        + "category_name text,"
                        + "category_img text,"
                        + "parent_id integer,"
                        + "category_weight integer,"
                        + "category_code integer,"
                        + "is_show integer,"

                        + "product_id integer,"
                        + "product_name text,"

                        + "version integer"
                        + " );");

            } else if (newVersion == 6) {
                db.execSQL("drop TABLE IF EXISTS " + DATABASE_TABLE_PRODUCT_CATEGORY);
                // 商品大类型     f1:color   f2:bold   f3:triangle
                db.execSQL("CREATE TABLE " + DATABASE_TABLE_PRODUCT_CATEGORY + "("
                        + "id integer primary key not null,"
                        + "category_name text,"
                        + "category_img" + " text,"
                        + "parent_id" + " INTEGER,"
                        + "category_weight INTEGER,"
                        + "f1 text,"
                        + "f2 text,"
                        + "f3 text,"
                        + "f4 text,"
                        + "f5 text,"
                        + "f6 text,"
                        + "f7 text,"
                        + "f8 text"
                        + ");");
            } else if (newVersion == 7) {
                //删除img表，重新创建，并插入字段is_thumbnail 1：是缩略图  0：不是
                db.execSQL("drop TABLE IF EXISTS " + DATABASE_TABLE_PRODUCT_IMGS);
                db.execSQL("CREATE TABLE " + DATABASE_TABLE_PRODUCT_IMGS
                        + " ( " + "id NOT NULL,"
                        + "product_id" + " INTEGER DEFAULT NULL,"
                        + "img_id" + " INTEGER,"
                        + "is_thumbnail" + " INTEGER"
                        + " );");
            } else if (newVersion == 8) {
                db.execSQL("drop TABLE IF EXISTS " + DATABASE_TABLE_PRODUCT_SHOPPING_CAR);
                db.execSQL("CREATE TABLE " + DATABASE_TABLE_PRODUCT_SHOPPING_CAR + " ( "
                        + "product_id" + " INTEGER PRIMARY KEY NOT NULL,"
                        + "user_id" + " INTEGER NOT NULL,"
                        + "goods_object" + " text,"
                        + "temp_field" + " text"
                        + " );");
                //drop table 删除表
                //delete from db  删除数据
            } else if (newVersion == 9) {
                //chat_msg 聊天记录   新建记录表
                db.execSQL("CREATE TABLE " + DATABASE_TABLE_CHAT_MSG + " ( "
                        + "id" + " integer NOT NULL PRIMARY KEY AUTOINCREMENT,"
                        + "message_code" + " text,"
                        + "user_id" + " text,"
                        + "object_entity" + " text,"
                        + "create_time" + " DATETIME,"
                        + "temp_field" + " text"
                        + " );");
                db.execSQL("drop TABLE IF EXISTS " + DATABASE_TABLE_PRODUCT_SHOPPING_CAR);
                db.execSQL("CREATE TABLE " + DATABASE_TABLE_PRODUCT_SHOPPING_CAR + " ( "
                        + "product_id" + " INTEGER PRIMARY KEY NOT NULL,"
                        + "user_id" + " INTEGER NOT NULL,"
                        + "goods_object" + " text,"
                        + "product_type" + " INTEGER,"
                        + "temp_field" + " text"
                        + " );");
            }

            //若要添加新标，直接create table

            /*db.execSQL("drop TABLE IF EXISTS" + DATABASE_TABLE_PRODUCTS);
            db.execSQL("drop TABLE IF EXISTS" + DATABASE_TABLE_PRODUCT_SHOPPING_CAR);
            db.execSQL("drop TABLE IF EXISTS" + DATABASE_TABLE_PRODUCT_CATEGORY);
            db.execSQL("drop TABLE IF EXISTS" + DATABASE_TABLE_DICTTYPE);
            db.execSQL("drop TABLE IF EXISTS" + DATABASE_TABLE_DICTDATA_PAY);
            db.execSQL("drop TABLE IF EXISTS"
                    + DATABASE_TABLE_DICTDATA_ORDERSTATUS);
            db.execSQL("drop TABLE IF EXISTS" + DATABASE_TABLE_PRODUCT_FAVORITE);
            db.execSQL("drop TABLE IF EXISTS" + DATABASE_TABLE_DELIVERY_ADDRESS);
            db.execSQL("drop TABLE IF EXISTS" + DATABASE_TABLE_DELIVERY_AREA);
            db.execSQL("drop TABLE IF EXISTS"
                    + DATABASE_TABLE_DELIVERY_COMMUNITY);
            db.execSQL("drop TABLE IF EXISTS" + DATABASE_TABLE_SEARCH_PRODUCT_HISTORY);
            db.execSQL("drop TABLE IF EXISTS" + DATABASE_TABLE_PRODUCT_IMGS);
            onCreate(db);*/

            /*if(oldVersion < 2) {
                db.execSQL("ALTER TABLE pedant ADD COLUMN student_name text");
            }*/

            //比如,为 Subscription添加两个栏目
            /*ALTER TABLE Subscription ADD COLUMN Activation BLOB;
            ALTER TABLE Subscription ADD COLUMN Key BLOB;*/
        }

    }

    /**
     * 按照id查询某条记录--购物车查询
     */
    public GoodsObject getRecordFromProductId(int product_id) {
        mDb = mDBHelper.getReadableDatabase();
        Cursor cursor = mDb.rawQuery("select * from "
                        + DATABASE_TABLE_PRODUCT_SHOPPING_CAR + " where " + "product_id=?",
                new String[]{String.valueOf(product_id)});
        String productObjectString = "";
        GoodsObject product = null;
        if (cursor != null) {
            while (cursor.moveToNext()) {
                productObjectString = cursor.getString(2);
            }
            cursor.close();
            mDb.close();
            product = (GoodsObject) CommunityTool.readObject(productObjectString);
            if (product != null) {
                return product;
            } else {
                product = new GoodsObject();
                Log.d(TAG, "gouwwuche is null ==> new product");
            }
        }
        return product;
    }

}
